<?php
/**
 * Created by PhpStorm.
 * User: bevan
 * Date: 2018/1/4
 * Time: 上午10:07
 */

namespace app\admin\controller;

use app\admin\facade\User;
use app\common\controller\Admin;
use think\facade\Request;
use think\facade\Config;

/**
 * @title 【后台】用户管理
 */
class UserController extends Admin
{

    /**
     * @title 获取所有用户
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @param  {"name":"page","type":"int","required":true,"default":"1","desc":"页码"}
     * @param  {"name":"num","type":"int","required":true,"default":"15","desc":"每页条数"}
     * @param  {"name":"type","type":"string","required":true,"default":"","desc":"用户类型,传值方式：1,2 含义：1代表管理，2代表普通用户"}
     * @return {"name":"code","type":"int","required":true,"desc":"返回状态码","level":1}
     * @return {"name":"msg","type":"string","required":true,"desc":"返回信息","level":1}
     * @return {"name":"data","type":"json","required":true,"desc":"获取到的数据","level":1}
     * @return {"name":"users","type":"array","required":true,"desc":"用户数据","level":2}
     * @return {"name":"total","type":"string","required":true,"desc":"该类型的用户总量","level":2}
     */
    public function index()
    {
        $data['page'] = Request::has('page') ? Request::param('page') : 1;
        $data['limit'] = Request::has('limit') ? Request::param('limit') : 15;
        $data['type'] = Request::has('type') ? explode(',', Request::param('type')) : [];
        $data['keyword'] = Request::has('keyword') ? Request::param('keyword') : '';
        return User::getList($data);
    }

    /**
     * @title 读取单个用户 (不带权限信息, 管理员读取的用户信息)
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @param {"name":"id","type":"int","required":true,"desc":"用户主键"}
     * @return {"name":"code","type":"json","required":true,"desc":"状态码","level":1}
     * @return {"name":"msg","type":"string","required":true,"desc":"状态信息","level":1}
     * @return {"name":"data","type":"array","required":true,"desc":"该用户的详细信息","level":1}
     */
    public function read($id)
    {
        return User::index($id);
    }

    /**
     * @title 新增用户（后台管理员操作）
     * @desc  {"0":"接口地址：","1":"请求方式：POST","2":"接口备注："}
     * @param {"name":"1","type":"string","required":false,"default":"","desc":""}
     * @return {"name":"1","type":"json","required":true,"desc":"","level":""}
     */
    public function save()
    {
        $data['user_login'] = Request::has('user_login') ? Request::param('user_login') : '';
        $data['user_pass'] = Request::has('user_pass') ? Request::param('user_pass') : '';
        $data['user_email'] = Request::has('user_email') ? Request::param('user_email') : '';
        $data['mobile'] = Request::has('mobile') ? Request::param('mobile') : '';
        $data['status'] = Request::has('status') ? Request::param('status') : '';

        //验证
        $result = $this->validate(
            $data,
            'app\admin\validate\UserValidate.user_login'
        );  // 使用登录需求的验证

        if (true !== $result) {
            return ['code' => 4001, 'msg' => $result, 'data' => []];
        }

        return User::add($data);
    }

    /**
     * @title 获取对应用户可编辑内容 (超级管理员 或 用户自己本身)
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @param {"name":"1","type":"string","required":false,"default":"","desc":""}
     * @return {"name":"1","type":"json","required":true,"desc":"","level":""}
     */
    public function edit($id)
    {
        return User::edit($id);
    }

    /**
     * @title 更新对应用户 （管理员更新对应用户）
     * @desc  {"0":"接口地址：","1":"请求方式：POST","2":"接口备注："}
     * @param {"name":"user_login","type":"string","required":true,"default":"","desc":"用户名"}
     * @param {"name":"user_pass","type":"string","required":true,"default":"","desc":"用户密码明文"}
     * @param {"name":"user_email","type":"string","required":true,"default":"","desc":"用户邮件"}
     * @param {"name":"rules","type":"string","required":false,"default":"","desc":"授权权限,样式:1,2,3"}
     * @return {"name":"code","type":"json","required":true,"desc":"状态码","level":"1"}
     * @return {"name":"msg","type":"json","required":true,"desc":"状态信息","level":"1"}
     * @return {"name":"data","type":"json","required":true,"desc":"返回的数据","level":"1"}
     */
    public function update($id)
    {
        // 手机 邮箱 可以不填
        $data['user_login'] = Request::has('user_login') ? Request::param('user_login') : '';
        $data['user_pass'] = Request::has('user_pass') ? Request::param('user_pass') : '';
        $data['status'] = Request::has('status') ? Request::param('status') : '';
        $data['mobile'] = Request::has('mobile') ? Request::param('mobile') : '';
        $data['user_email'] = Request::has('user_email') ? Request::param('user_email') : '';
        //数据验证
        if ($data['user_pass']) {
            $result = $this->validate(
                Request::param(),
                'app\admin\validate\UserValidate.user_login'
            );
        } else {
            $result = $this->validate(
                Request::param(),
                'app\admin\validate\UserValidate.admin_edit_user'
            );
        }

        if (true !== $result) {
            return ['code' => 4001, 'msg' => $result, 'data' => []];
        }

        return User::update($id, $data);
    }

    /**
     * @title 删除指定用户 (删除方式待定, 未开放)
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @param {"name":"1","type":"string","required":false,"default":"","desc":""}
     * @return {"name":"code","type":"json","required":true,"desc":"状态码","level":"1"}
     * @return {"name":"msg","type":"json","required":true,"desc":"状态信息","level":"1"}
     * @return {"name":"data","type":"json","required":true,"desc":"返回的数据","level":"1"}
     */
    public function delete($id)
    {
        return User::delete($id);
    }

    /**
     *
     * @method 获取管理员列表
     * @version
     * @User: bevan
     * @return
     */
    public function getAdmins()
    {
        $data['page'] = Request::has('page') ? Request::param('page') : 1;
        $data['limit'] = Request::has('limit') ? Request::param('limit') : 15;
        $data['keyword'] = Request::has('keyword') ? Request::param('keyword') : '';
        return User::getAdmins($data);
    }


    /**
     *
     * @method 添加管理员
     * @version
     * @User: bevan
     * @return
     */
    public function addAdmin()
    {
        $data['user_login'] = Request::has('user_login') ? Request::param('user_login') : '';
        $data['user_pass'] = Request::has('user_pass') ? Request::param('user_pass') : '';
        $data['user_email'] = Request::has('user_email') ? Request::param('user_email') : '';
        $data['group_id'] = Request::has('group_id') ? Request::param('group_id') : '';

        //验证 添加操作人员 是否有 权限 设置 权限是否足够
        // todo

        if (!$data['group_id']) {
            return ['code' => 4003, 'msg' => '角色未设置', 'data' => []];
        }

        $result = $this->validate(
            $data,
            'app\admin\validate\UserValidate.add_admin'
        );
        if (true !== $result) {
            return [
                'code' => 4003,
                'msg' => $result,
                'data' => []
            ];
        }

        return User::addAdmin($data);
    }

    /**
     *
     * @method 获取管理员信息
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function getAdminInfo($id)
    {
        cmf_check_primary_key_is_number($id);

        return User::getAdminInfo($id);
    }

    /**
     *
     * @method 更新管理员
     * @version
     * @User: bevan
     * @return
     */
    public function updateAdmin()
    {
        //验证
        $data['user_login'] = Request::has('user_login') ? Request::param('user_login') : '';
        $data['user_pass'] = Request::has('user_pass') ? Request::param('user_pass') : '';
        $data['user_email'] = Request::has('user_email') ? Request::param('user_email') : '';
        $data['group_id'] = Request::has('group_id') ? Request::param('group_id') : '';
        $data['id'] = Request::has('id') ? Request::param('id') : '';

        cmf_check_primary_key_is_number($data['id']);

        //校验
        if (!$data['user_pass']) {
            $result = $this->validate($data, 'app\admin\validate\UserValidate.update_admin_without_pass');
        } else {
            $result = $this->validate($data, 'app\admin\validate\UserValidate.update_admin_with_pass');
        }

        if (true !== $result) {
            return [
                'code' => 4003,
                'msg' => $result,
                'data' => []
            ];
        }
        //更新
        return User::updateAdmin($data);
    }

    /**
     *
     * @method 删除管理员用户（硬删除）
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function delAdmin($id)
    {
        cmf_check_primary_key_is_number($id);

        if ($id == 1) {
            return ['code'=>4001,'msg'=>"不允许删除超级管理员",'data'=>[]];
        }

        return User::delAdmin($id);
    }

    /**
     * @title 切换用户状态
     */
    public function toggle($id)
    {
        cmf_check_primary_key_is_number($id);

        return User::toggle($id);
    }

    /**
     *
     * @method 搜索
     * @version
     * @User: bevan
     * @return
     */
    public function searchUser()
    {
        return User::searchUser(Request::param('name'));
    }
}
